package com.example.library;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DatabaseHelper extends SQLiteOpenHelper {
    private static final String DB_NAME = "library.db";
    private static final int DB_VERSION = 1;

    public DatabaseHelper(Context context) {
        super(context, DB_NAME, null, DB_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        try {
            // 创建用户表
            db.execSQL("CREATE TABLE IF NOT EXISTS user (" +
                    "id INTEGER PRIMARY KEY AUTOINCREMENT, " +
                    "name TEXT NOT NULL, " +
                    "studentId TEXT NOT NULL, " +
                    "password TEXT NOT NULL, " +
                    "contact TEXT, " +
                    "userType TEXT NOT NULL, " +
                    "creditScore INTEGER DEFAULT 100, " +
                    "status INTEGER DEFAULT 1)");

            // 创建座位表
            db.execSQL("CREATE TABLE IF NOT EXISTS seat (" +
                    "id INTEGER PRIMARY KEY AUTOINCREMENT, " +
                    "area TEXT NOT NULL, " +
                    "number TEXT NOT NULL, " +
                    "type TEXT NOT NULL, " +
                    "hasPower INTEGER DEFAULT 0, " +
                    "status INTEGER DEFAULT 1)");

            // 创建预约表
            db.execSQL("CREATE TABLE IF NOT EXISTS reservation (" +
                    "id INTEGER PRIMARY KEY AUTOINCREMENT, " +
                    "userId INTEGER NOT NULL, " +
                    "seatId INTEGER NOT NULL, " +
                    "startTime TEXT NOT NULL, " +
                    "endTime TEXT NOT NULL, " +
                    "status TEXT NOT NULL, " +
                    "checkinTime TEXT)");

            // 添加默认管理员账户
            ContentValues adminValues = new ContentValues();
            adminValues.put("name", "管理员");
            adminValues.put("studentId", "admin");
            adminValues.put("password", "admin123");
            adminValues.put("userType", "管理员");
            adminValues.put("creditScore", 100);
            adminValues.put("status", 1);
            db.insert("user", null, adminValues);

            // 添加测试用学生账户
            ContentValues studentValues = new ContentValues();
            studentValues.put("name", "测试学生");
            studentValues.put("studentId", "test001");
            studentValues.put("password", "123456");
            studentValues.put("contact", "13800138000");
            studentValues.put("userType", "学生");
            studentValues.put("creditScore", 100);
            studentValues.put("status", 1);
            db.insert("user", null, studentValues);

        } catch (Exception e) {
            Log.e("DatabaseHelper", "Error creating database: " + e.getMessage(), e);
        }
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        try {
            // 如果需要升级数据库结构，在这里添加升级逻辑
            if (oldVersion < 2) {
                // 未来版本升级时在这里添加新的表或字段
            }
        } catch (Exception e) {
            Log.e("DatabaseHelper", "Error upgrading database: " + e.getMessage(), e);
        }
    }

    // 检查数据库是否正确初始化
    public boolean checkDatabaseIntegrity(Context context) {
        SQLiteDatabase db = null;
        try {
            db = this.getReadableDatabase();
            // 检查user表是否存在
            db.rawQuery("SELECT * FROM user LIMIT 1", null);
            return true;
        } catch (Exception e) {
            Log.e("DatabaseHelper", "Database integrity check failed: " + e.getMessage(), e);
            // 如果检查失败，尝试重新创建数据库
            context.deleteDatabase(DB_NAME);
            return false;
        } finally {
            if (db != null) {
                db.close();
            }
        }
    }
} 